home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / mkdirlist < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.8 KB  |  170 lines

  1. #!/bin/ksh
  2. # @(#) mkdirlist.ksh 2.0 96/01/19
  3. # 94/10/27 John H. DuBois III (john@armory.com)
  4. # 94/11/15 Added all options.
  5. # 94/11/21 Fixed mount option.  Added -u to sort options.
  6. # 95/09/17 Added a option.
  7. # 96/01/19 Changed default tmpdir to home dir for safety.
  8. # 96/04/09 Added x option.
  9.  
  10. # Output format: 
  11. # directory-name full-directory-path
  12.  
  13. lng_name=${0##*/}
  14. Usage="Usage: $lng_name [-adhm] [-o<output-file>] [directory|filename  ...]"
  15. Output=$HOME/.dirs
  16. Mount=-mount
  17. DoFind=true
  18. Append=false
  19. Debug=false
  20.  
  21. while getopts :ahmdo:x opt; do
  22.     case $opt in
  23.     h)
  24.     echo \
  25. "$lng_name: make a file of directory names suitable for searching with 'look'.
  26. The output file contains one line for each directory, with two words on it. 
  27. The first word is the last directory component; the second is the full path to
  28. the directory (including the last component).  The output file is sorted. 
  29. Because the last component is at the start of the line and the lines are
  30. sorted, the look command can quickly find any directory that begins with a
  31. given prefix.  A file in this format is used by the 'ucd' change-directory
  32. Korn shell function.
  33. $Usage
  34. If a list of directories is given, 'find' is run on them to find all
  35. directories below them.  The -mount flag is included (filesystems mounted
  36. below the named directories will not be searched).  The resulting directory
  37. list is processed as described above and the output is stored in the output
  38. file (\"\$HOME/.dirs\" by default; this is where 'ucd' expects to find it).
  39. If the -d flag is given, the arguments are taken to be files of directory
  40. lists, one directory name per line (as produced by e.g. 'find / -type d').  If
  41. -d is given without any command-line arguments, a list of directories is read
  42. from the standard input.  In either case, the directory lists are processed as
  43. described above and stored in the output file.  If one of the input files is
  44. the same as the output file, it will be read correctly before being
  45. overwritten.
  46. Other options:
  47. -a: Append to the output to the output file, if it already exists.
  48. -h: Print this help.
  49. -m: Do not give the -mount flag to find.
  50. -o<output-file>: Store the output in output-file."
  51.        exit 0
  52.        ;;
  53.     a)
  54.     Append=true
  55.     ;;
  56.     o)
  57.     Output=$OPTARG
  58.     ;;
  59.     m)
  60.     unset Mount
  61.     ;;
  62.     d)
  63.     DoFind=false
  64.     ;;
  65.     x)
  66.     Debug=true
  67.     print -u2 "Debugging is on."
  68.     ;;
  69.     +?)
  70.     print -u2 "$lng_name: options should not be preceded by a '+'."
  71.     exit 1
  72.     ;;
  73.     :) 
  74.     print -r -u2 -- \
  75.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  76.     exit 1
  77.     ;;
  78.     ?) 
  79.     print -u2 "$lng_name: $OPTARG: bad option.  Use -h for help."
  80.     exit 1
  81.     ;;
  82.     esac
  83. done
  84.  
  85. # In early ksh, false is an alias and so is not run when a shell var is
  86. # expanded to it; in these versions unalias false returns 0 so can use this as
  87. # a test to make it a function instead, which *is* run when expanded from var.
  88. unalias false && function false { return 1; }
  89.  
  90. # remove args that were options
  91. let OPTIND=OPTIND-1
  92. shift $OPTIND
  93.  
  94. if [ $# -lt 1 ] && $DoFind; then
  95.     print -u2 "$Usage\nUse -h for help."
  96.     exit
  97. fi
  98.  
  99. NZSize=false
  100.  
  101. : ${TMP:=$TMPDIR}
  102. : ${TMP:=$HOME}
  103. : ${TMP:=/tmp}
  104. if $DoFind; then
  105.     # Catch this because find doesn't care.
  106.     for dir; do
  107.     if [ ! -d "$dir" ]; then
  108.         print -u2 "$lng_name: $dir is not a directory.  Aborting."
  109.         exit 1
  110.     fi
  111.     done
  112.     tmpfile=$TMP/#mkdls.$$
  113.     $Debug && set -x
  114.     find "$@" -type d $Mount -print > $tmpfile
  115.     $Debug && set +x
  116.     set -- $tmpfile
  117.     [ -s "$tmpfile" ] && NZSize=true
  118. else
  119.     for file; do
  120.     $Debug && print -u2 "Checking $file"
  121.     if [ ! -a "$file" ]; then
  122.         print -u2 "$lng_name: cannot access $file.  Aborting."
  123.         exit 1
  124.     fi
  125.     # Do this test separately from read because read will fail for
  126.     # empty file.
  127.     if [ ! -r "$file" ]; then
  128.         print -u2 "$lng_name: cannot read $file.  Aborting."
  129.         exit 1
  130.     fi
  131.     # This is mainly to make sure that we don't try to reprocess a dir list
  132.     # that is already in the output format.
  133.     read a1 a2 < "$file"
  134.     if [ -n "$a2" ]; then
  135.         print -u2 \
  136.         "$lng_name: $file is not a plain directory list.  Aborting."
  137.         exit 1
  138.     fi
  139.     [ -s "$file" ] && NZSize=true
  140.     done
  141. fi
  142.  
  143. # Don't overwrite old output file (if any) if we won't put anything in it.
  144. $NZSize || {
  145.     print -u2 "$lng_name: no directory names to process.  Aborting."
  146.     exit 1
  147. }
  148.  
  149. PATH=$PATH:/usr/local/bin    # make sure we can find gawk
  150.  
  151. $Append && OtherFiles=$Output || OtherFiles=
  152.  
  153. # Usage gawk because its %c works the way we want
  154. gawk -F/ '
  155. BEGIN {
  156.     for (i = 1; i <= 32; i++)
  157.     BadPat = BadPat sprintf("%c",i)
  158.     BadPat = "[" BadPat sprintf("%c",127) "]"
  159. }
  160.  
  161. # Ignore any dirs that have control chars or spaces in them.
  162. # Control chars are liable to muck up prompt if it includes $PWD,
  163. # spaces will confuse routines that search/use this file.
  164. $0 !~ BadPat {
  165.     printf "%s %s\n",$NF,$0
  166. }
  167. ' "$@" | sort -u -o $Output - $OtherFiles
  168.  
  169. $DoFind && rm $tmpfile
  170.